<?php
  error_reporting(0);
  session_start();
  /*
  if ($_SESSION["tipo"]!=1){
    header ("Location: index.php");
  }
   */
  include_once("seguridad.php");
  include_once('fachadaBD.php');
  $f   = new fachadaBD();
?>
<div class="AdminCompra">
<!--
  <?php
    $ret = $f->getCompra();
    $cont = 0;
    while ($row = mysql_fetch_assoc($ret)){
      $user = $f->getUserByEmail($row["USUARIO"]);
      $user = mysql_fetch_assoc($user);
      ?>
  <div id="perro" class="show_user">
    <table>
      <tr>
        <td class="show_user">
          <?php echo $user["NOMBRE"]." ".$user["APELLIDO"]; ?>
        </td>
        <td class="show_user">
          <?php echo $user["CIPHER"]; ?>
        </td>
      </tr>
      <tr>
        <td class="show_user">
          <?php echo $user["TELEFONO"]; ?>
        </td>
      </tr>
    </table>
  </div>
  <?php
    $cont++;
    }
  ?>
-->
<a href="comprasRevisadas.php">Compras Revisadas</a>
  <table class="AdminTableCompra">
    <?php
      $ret = $f->getCompra();
      // Comprobar resultado
      // El siguiente código muestra la consulta enviada a MySQL y el error ocurrido.
      //  Útil para debugging.
//    if (!$ret) {
//      $message = 'Invalid query: ' . mysql_error() . "\n";
//      $message .= 'Whole query: ' . $query;
//      die($message);
//    }
      $cont = 0;
      while ($row = mysql_fetch_assoc($ret)){
        if ($cont % 2 == 0){
          echo "<tr class=\"AdminContenidoCompra0\"> <td style=\"width: 75px;\">";
        }else{
          echo "<tr class=\"AdminContenidoCompra1\"> <td style=\"width: 75px;\">";
        }
        //$user = $f->getUserByEmail($row["USUARIO"]);
        $user = $f->getUserByEmail($row["USUARIO"]);
        $user = mysql_fetch_assoc($user);
        //$user = $f->sacarSlot();
        echo $row["PRODUCTO"];
        echo "</td> <td style=\"width: 75px;\">";
        echo $row["FECHA"];
        //echo "</td> <td style=\"width: 120px;\">";
        echo "</td> <td style=\"width: 70px;\">";
        echo $row["USUARIO"];
        echo "</td> <td style=\"width: 25px;\">";
        echo $row["CANTIDAD"];
        echo "</td> <td>";
        if ($row["TIPO_PAGO"] == 0){
          echo "Deposito";
        }else if ($row["TIPO_PAGO"] == 1){
          echo "Transferencia Electronica";
        }else if ($row["TIPO_PAGO"] == 2){
          echo "Tarjeta de Credito";
        }
        echo "</td> <td>";
        if ($row["TIPO_ENTREGA"] == 0){
          echo "MRW";
        }else if($row["TIPO_ENTREGA"] == 1){
          echo "Grupo Zoom";
        }else if($row["TIPO_ENTREGA"] == 2){
          echo "Retiro personal";
        }
        echo "</td> <td>";
        echo "<form name=\"fr".$cont."\" method=\"POST\" action =\"reprobar.php\" >";
        echo "<input name=\"id\" type=\"hidden\" value =".$row["ID"].">";
        echo "</form>";
        echo "<form name=\"fa".$cont."\" method=\"POST\" action =\"aprobar.php\" >";
        echo "<input name=\"id\" type=\"hidden\" value =".$row["ID"].">";
        echo "</form>";
        //echo "<input name=\"val\" type=\"submit\" value=\"si\" >";
        echo "<button onclick=\"verif('fa".$cont."')\">si";
        echo "</td> <td>";
        //echo "<input name=\"val\" type=\"submit\" VALUE=\"no\" >";
        echo "<button onclick=\"verif('fr".$cont."')\">no";
        echo "<button id=\"b".$cont."\" value=\"0\" onclick=\"show(".$cont.")\"></button> </td>";
        echo " </tr>";
        if ($cont % 2 == 0){
          echo "<tr id=\"".$cont."\" style=\"visibility: hidden;\" class=\"AdminContenidoCompra0\">";
//          echo "<tr id=\"".$cont."\" class=\"hid\"> ";
        }else{
          echo "<tr id=\"".$cont."\" class=\"AdminContenidoCompra1\" style=\"visibility: hidden;\">";
        }
        echo "<td> ".$user["NOMBRE"]." </td>";
        echo "<td> ".$user["APELLIDO"]."</td>";
        echo "<td> ".$user["CIPHER"]."</td>";
        echo "<td> ".$user["TELEFONO"]."</td>";
        echo "<td></td><td></td><td></td><td></td>";
        echo "</tr>";
        //echo "aqui ".$user["NOMBRE"]." aqui";
        ++$cont;
      }
    ?>
    <script language="JavaScript" type="text/javascript">
      function show(num){
        if (document.getElementById("b"+num).getAttribute("value") == 0){
          document.getElementById(num).setAttribute('style','visibility: visible');
          document.getElementById("b"+num).value = 1;
        }else{
          document.getElementById(num).setAttribute('style','visibility: hidden');
          document.getElementById("b"+num).value = 0;
        }
      }
      function verif(num){
        var reprobar=/fr\d*/;
        var aprobar=/fa\d*/;
        if (num.match(reprobar)){
          var answer = confirm("Desea reprobar la compra?");
        }else if(num.match(aprobar)){
          var answer = confirm("Desea aprobar la compra?");
        }
        if (answer){
          document.forms[num].submit();
        }
      }
    </script>
 </table>
</div>
